package com.kdtech.analyse.outside;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @name NBCNEWS
 * @link http://www.nbcnews.com/
 * @author dhy
 */
public class NBCNewsOutsideAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.nbcnews.com/[a-z]*/[a-z0-9\\-]+/[a-z0-9\\-]+-[a-z0-9\\-]+",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title =doc.select("h1[itemprop=headline]").text();

		Elements dateList = doc.select("time.timestamp_article");
		if(dateList.size() > 0){
			String dateStr = dateList.get(0).text();
			for(int i = 0; i < 2; i++)
				dateStr = StringUtils.substringBeforeLast(dateStr, " ");
			try {
				date = new SimpleDateFormat("MMM dd yyyy, HH:mm", Locale.US).parse(dateStr).getTime();
			} catch (ParseException e) {
				date = System.currentTimeMillis();
			}
		}
		
		content=HtmlCleaner.getContentHtml(url,doc.select("div.article-body"));

		author = doc.select("span.byline_author").text();
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
